﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EBA.Localization;

namespace EBA.Reporting.Spreadsheet
{
    /// <summary>
    /// 字母索引，用于电子表格的列索引
    /// </summary>
    public class LetterIndex : IComparable<LetterIndex>, IEquatable<LetterIndex>
    {
        /// <summary>
        /// 根据整型索引创建子母索引
        /// </summary>
        /// <param name="index">整型索引</param>
        public LetterIndex(int index)
        {
            this.Index = index;
        }

        /// <summary>
        /// 根据字母索引创建字母索引
        /// </summary>
        /// <param name="name">字母索引</param>
        public LetterIndex(string name)
        {
            this.Name = name;
        }

        int _Index;
        /// <summary>
        /// 整型索引
        /// </summary>
        public int Index
        {
            get
            {
                return this._Index;
            }
            set
            {
                this._Name = ParseToLetter(value);
                this._Index = value;
            }
        }

        string _Name;
        /// <summary>
        /// 子母索引
        /// </summary>
        public string Name
        {
            get
            {
                return this._Name;
            }
            set
            {
                this._Index = ParseToIndex(value);
                this._Name = value;
            }
        }

        /// <summary>
        /// 与<paramref name="letter"/>比较大小。
        /// </summary>
        /// <param name="letter">字母索引</param>
        /// <returns></returns>
        public int CompareTo(LetterIndex letter)
        {
            return letter == null ? 1 : this.Index.CompareTo(letter.Index);
        }

        /// <summary>
        /// 判断和<paramref name="letter"/>的值是否相等。
        /// </summary>
        /// <param name="letter">字母索引</param>
        /// <returns></returns>
        public bool Equals(LetterIndex letter)
        {
            return letter == null ? false : this.Index.Equals(letter.Index);
        }


        static int ParseToIndex(string columnName)
        {
            int index = 0;

            var chars = columnName.ToUpperInvariant().ToCharArray().Reverse().ToArray();
            for (var i = 0; i < chars.Length; i++)
            {
                var c = (chars[i] - 64);
                index += i == 0 ? c : (c * (int)Math.Pow(26, i));
            }

            if (index < 0)
            {
                throw new ArgumentOutOfRangeException("the index must start with zero.".T());
            }


            return index;
        }

        static string ParseToLetter(int column)
        {
            if (column < 0)
            {
                throw new ArgumentOutOfRangeException("the index must start with zero.".T());
            }


            int dividend = column;
            string columnName = String.Empty;
            int modulo;

            while (dividend > 0)
            {
                modulo = (dividend - 1) % 26;
                columnName = Convert.ToChar(65 + modulo).ToString() + columnName;
                dividend = (int)((dividend - modulo) / 26);
            }

            return columnName;
        }
    }
}
